﻿/* ==============================================================================
* 功能描述：RoomManager
* 创 建 者：青金石
* 创建日期：2020/6/2 17:11:44
* ==============================================================================*/

using System.Collections.Generic;

public class RoomManager
{
    //最大id，房间数
    private static int maxID = 0;

    //房间列表
    public static Dictionary<int, Room> rooms = new Dictionary<int, Room>();

    //获取房间
    public static Room GetRoom(int id)
    {
        Room room;
        if (rooms.TryGetValue(id, out room) && room != null)
        {
            return room;
        }
        else
        {
            return null;
        }
    }

    //添加房间
    public static Room AddRoom()
    {
        maxID++;
        Room room = new Room();
        room.id = maxID;
        rooms.Add(maxID, room);
        return room;
    }

    //删除房间
    public static bool RemoveRoom(int id)
    {
        if (!rooms.ContainsKey(id))
        {
            return false;
        }
        rooms.Remove(id);
        return true;
    }

    //生成MsgGetRoomList协议
    public static MsgBase ToMsg()
    {
        MsgGetRoomList msg = new MsgGetRoomList();
        RoomInfo[] roomList = new RoomInfo[rooms.Count];
        msg.rooms = roomList;
        int i = 0;
        foreach (Room room in rooms.Values)
        {
            RoomInfo roomInfo = new RoomInfo();
            roomInfo.id = room.id;
            roomInfo.count = room.playerIds.Count;
            roomInfo.status = (int)room.status;
            roomInfo.capacity = room.maxCount;
            roomList[i] = roomInfo;
            i++;
        }
        return msg;
    }

    /// <summary>
    /// 判断所有房间的胜负情况
    /// </summary>
    public static void Update()
    {
        foreach (Room room in rooms.Values)
        {
            room.Update();
        }
    }
}